package com.hanko.rabbitmq;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Hanko
 * @Date: 2022-3-3 18:03
 */
@RestController
@RequiredArgsConstructor
public class OrderSendMessage {

    private final RabbitTemplate rabbitTemplate;

    @RequestMapping("send")
    public String sendMessage(String delayTime,String index){
        String msg = "发送第"+index+"条消息！" + delayTime;
        //将消息携带路由键值
        rabbitTemplate.convertAndSend(DelayQueueRabbitConfig.ORDER_EXCHANGE, DelayQueueRabbitConfig.ORDER_ROUTING_KEY,
                msg,message->{
                    //毫秒为单位
                    message.getMessageProperties().setExpiration(delayTime);
                    return message;
                });
        return msg;
    }
}
